package com.smasher.doc.operation;

import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.RawRes;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

public class OperationWriteHelper {


    private static final String TAG = "OperationWriteHelper";


    /**
     * 方式1
     *
     * @param resources resources
     * @param parent    parent
     * @param id        id
     */
    public void fillDocFromRaw(
            final Resources resources,
            @RawRes int id,
            HashMap<String, String> contentMap,
            File parent,
            String targetName
    ) {
        GenerateInputStream stream = new TargetInputStream() {
            @Override
            public InputStream generateRaw(Resources resources, int id) throws IOException {
                return resources.openRawResource(id);
            }
        };

        fillDocActualRaw(stream, resources, id, contentMap, parent, targetName);
    }


    private void fillDocActualRaw(
            GenerateInputStream stream,
            final Resources resources,
            @RawRes int id,
            HashMap<String, String> contentMap,
            File parent,
            String targetName
    ) {
        try (InputStream inputStream = stream.generateRaw(resources, id)) {
            readActual(contentMap, parent, targetName, inputStream);
        } catch (IOException e) {
            Log.e(TAG, "readDocFromRaw: ", e);
        }
    }

    private void readActual(HashMap<String, String> contentMap, File parent, String targetName, InputStream inputStream) throws IOException {
        XWPFDocument dcx = new XWPFDocument(inputStream);
        List<XWPFParagraph> paragraphs = dcx.getParagraphs();

        File targetFile = new File(parent, targetName);
        if (targetFile.exists()) {
            boolean deletedResult = targetFile.delete();
        }
        boolean createdResult = targetFile.createNewFile();
        // 获取文件输出流
        try (FileOutputStream fileOutputStream = new FileOutputStream(targetFile)) {
            int size = paragraphs.size();
            Log.i("Paragraphs count", String.valueOf(size));
            for (int i = 0; i < size; i++) {
                XWPFParagraph para = paragraphs.get(i);
                int runCount = para.getRuns().size();
                if (runCount == 0) {
                    continue;
                }
                Log.i("Operation", "Paragraphs start ==========================");
                for (XWPFRun run : para.getRuns()) {
                    String content = run.getText(0);
                    if (!TextUtils.isEmpty(content)) {
                        Log.i("Operation", content);
                        if ("灵武市公安局交通管理大队".equals(content)) {
                            run.setText("银川市公安局交通管理大队", 0);
                        }
                    }
                }
                Log.i("Operation", "Paragraphs end --------------------------");
            }
            dcx.write(fileOutputStream);
        }
    }

}
